﻿// -----------------------------------------------------------------------------
// DM 是 .NET 5 平台下开发管理的系统。
// Copyright © 2021 DM, 虫儿飞科技 Co.,Ltd.
//
// 框架名称：Furion
// 框架版本：1.0.0
// -----------------------------------------------------------------------------

using System.Collections.Generic;
using System.Linq;

using Z.Linq;

namespace Tool
{
    public static class DtreeExtensions
    {
        //public static Dtree GetDtree(this List<DtreeEntity> DtreeEntities, string pid)
        //{
        //    var dtree = new Dtree();
        //    dtree.data = GeDtreeItems(DtreeEntities, pid);
        //    return dtree;
        //}
        public static Dtree GetDtree(this IEnumerable<DtreeEntity> DtreeEntities, string pid)
        {
            var dtree = new Dtree();

            dtree.data = GeDtreeItems(DtreeEntities, pid);
            return dtree;
        }

        private static List<DtreeItem> GeDtreeItems(IEnumerable<DtreeEntity> entities, string pid)
        {
            List<DtreeItem> dtreeItems = new List<DtreeItem>();
            foreach (var dataconfig in entities.Where(o => o.pid == pid).OrderBy(o => o.sort))
            {
                DtreeItem dtreeitem = new DtreeItem()
                {
                    id = dataconfig.id,
                    title = dataconfig.name.Trim(),
                    parentId = pid
                };

                if (entities.Any(o => o.pid == dataconfig.id))
                {
                    dtreeitem.last = false;
                    dtreeitem.children = GeDtreeItems(entities, dataconfig.id);
                }
                else
                {
                    dtreeitem.last = true;
                }
                dtreeItems.Add(dtreeitem);
            }
            return dtreeItems;
        }

        public static Dtree GetDtreeCheck(this IEnumerable<DtreeEntity> DtreeEntities, string pid, IEnumerable<DtreeEntity> selectEntitites)
        {
            var dtree = new Dtree();
            dtree.data = GeDtreeCheckItems(DtreeEntities, pid, selectEntitites);
            return dtree;
        }

        private static List<DtreeCheckItem> GeDtreeCheckItems(IEnumerable<DtreeEntity> entities, string pid, IEnumerable<DtreeEntity> selectEntitites)
        {
            List<DtreeCheckItem> dtreeItems = new List<DtreeCheckItem>();
            foreach (var dataconfig in entities.Where(o => o.pid == pid).OrderBy(o => o.sort))
            {
                DtreeCheckItem dtreeitem = new DtreeCheckItem()
                {
                    id = dataconfig.id,
                    title = dataconfig.name.Trim(),
                    parentId = pid
                };

                if (entities.Any(o => o.pid == dataconfig.id))
                {
                    dtreeitem.last = false;
                    dtreeitem.children = GeDtreeCheckItems(entities, dataconfig.id, selectEntitites);
                }
                else
                {
                    dtreeitem.last = true;
                }
                dtreeItems.Add(dtreeitem);
            }
            return dtreeItems;
        }
    }
}